Standardized bar chart#

Setup#

import pandas as pd
import altair as alt
import warnings

warnings.simplefilter(action='ignore', category=FutureWarning)
alt.data_transformers.disable_max_rows()
DataTransformerRegistry.enable('default')

Data#

ROOT = "https://raw.githubusercontent.com/kirenz/datasets/master/"
DATA = "loans.csv"

df = pd.read_csv(ROOT + DATA)

df.homeownership = df.homeownership.astype("category")
df.application_type = df.application_type.astype("category")

Chart#

alt.Chart(df).mark_bar().encode(
    x=alt.X('homeownership',
          sort='-y',
          axis=alt.Axis(title="Homeownership", 
                        titleAnchor="start", 
                        labelAngle=0)),
    y=alt.Y('count(homeownership)', 
            stack="normalize", 
            sort="ascending", 
            axis=alt.Axis(title = "Count", 
                          titleAnchor="end")),
    color= alt.Color('application_type', 
                     legend=alt.Legend(title="Type"))
).properties(
    title='This is a standardized bar chart',
    width=300,
    height=150
).configure_title(
    fontSize=16,
    font='Arial',
    anchor='start',
    color='black'
)